/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the koala4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @Author: GuoYang
 */
package com.koalacloud.koala.zhaikucloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koalacloud.koala.zhaikucloud.entity.MallPanel;
import com.koalacloud.koala.zhaikucloud.entity.MallPanelContent;
import com.koalacloud.koala.zhaikucloud.entity.MallProduct;
import com.koalacloud.koala.zhaikucloud.vo.MallPanelContentVO;
import com.koalacloud.koala.zhaikucloud.vo.MallPanelVO;
import com.koalacloud.koala.zhaikucloud.mapper.MallPanelContentMapper;
import com.koalacloud.koala.zhaikucloud.mapper.MallPanelMapper;
import com.koalacloud.koala.zhaikucloud.mapper.MallProductMapper;
import com.koalacloud.koala.zhaikucloud.service.MallPanelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 版块分类
 *
 * @Author: GuoYang
 * @date 2022-02-10 15:33:09
 */
@Service
@RequiredArgsConstructor
public class MallPanelServiceImpl extends ServiceImpl<MallPanelMapper, MallPanel> implements MallPanelService {

    private final MallPanelContentMapper mallPanelContentMapper;

    private final MallProductMapper mallProductMapper;

    @Override
    public List<MallPanelVO> getPanelAll() {
        List<MallPanelVO> panelVOList = new ArrayList<MallPanelVO>();

        // 查询mall_panel中position=0 status=1
        List<MallPanel> panelList = baseMapper.selectList(Wrappers.<MallPanel>lambdaQuery()
                .eq(MallPanel::getPosition, 0).eq(MallPanel::getStatus, 1)
                .orderByAsc(MallPanel::getSortOrder));
        // 根据mall_panel中的id查询mall_panel_content中的panelid
        for(MallPanel mallPanel : panelList) {
            MallPanelVO panelVO = new MallPanelVO();

            List<MallPanelContentVO> panelContentVOList = new ArrayList<MallPanelContentVO>();

            List<MallPanelContent> panelContentList = mallPanelContentMapper.selectList(Wrappers.<MallPanelContent>lambdaQuery()
                    .eq(MallPanelContent::getPanelId, mallPanel.getId()).orderByAsc(MallPanelContent::getSortOrder));

            // 为面板数据关联商品信息
            for(MallPanelContent panelContent : panelContentList) {
                MallPanelContentVO panelContentVO = new MallPanelContentVO();
                // 查询商品id如果不为空，则提取相应商品信息
//                if (StrUtil.isNotBlank(panelContent.getProductId().toString())) {
                if (ObjectUtil.isNotNull(panelContent.getProductId())) {
                    MallProduct mallProduct = mallProductMapper.selectOne(Wrappers.<MallProduct>lambdaQuery()
                            .eq(MallProduct::getId, panelContent.getProductId()));

                    panelContentVO.setTitle(mallProduct.getTitle());
                    panelContentVO.setSellPoint(mallProduct.getSellPoint());
                    panelContentVO.setPrice(mallProduct.getPrice());
                    panelContentVO.setImage(mallProduct.getImage());
                }
                BeanUtil.copyProperties(panelContent, panelContentVO);
                panelContentVOList.add(panelContentVO);
            }

            BeanUtil.copyProperties(mallPanel, panelVO);
            panelVO.setPanelContents(panelContentVOList);
            panelVOList.add(panelVO);
        }

        return panelVOList;
    }

    @Override
    public List<Tree<Integer>> getTree(boolean lazy, Integer parentId) {
        if (!lazy) {
            List<TreeNode<Integer>> collect = baseMapper
                    .selectList(Wrappers.<MallPanel>lambdaQuery().orderByAsc(MallPanel::getSortOrder)).stream()
                    .map(getNodeFunction()).collect(Collectors.toList());

            return TreeUtil.build(collect, 0);
        }

        Integer parent = parentId == null ? 0 : parentId;

        List<TreeNode<Integer>> collect = baseMapper
                .selectList(Wrappers.<MallPanel>lambdaQuery().orderByAsc(MallPanel::getSortOrder))
                .stream().map(getNodeFunction()).collect(Collectors.toList());

        return TreeUtil.build(collect, parent);
    }

    private Function<MallPanel, TreeNode<Integer>> getNodeFunction() {
        return menu -> {
            TreeNode<Integer> node = new TreeNode<>();
            node.setId(menu.getId());
            node.setName(menu.getName());
            node.setParentId(0);
            node.setWeight(menu.getSortOrder());
            // 扩展属性
            Map<String, Object> extra = new HashMap<>();
            extra.put("type", menu.getType());
            extra.put("label", menu.getName());
            extra.put("sortOrder", menu.getSortOrder());
            node.setExtra(extra);
            return node;
        };
    }


}
